package rs.dalao.syc.passport.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import rs.dalao.syc.common.ex.ServiceException;
import rs.dalao.syc.common.response.JsonResult;
import rs.dalao.syc.common.response.ServiceCode;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;

@Slf4j
@RestControllerAdvice//由该注解来连接和controller控制器，捕获控制器抛出的异常
public class GlobalExceptionHandler {

    @ExceptionHandler
    /*处理自定义异常*/
    public JsonResult xx(ServiceException e){
        log.warn("由全局异常处理器开始处理RuntimeException");
        return JsonResult.fail(e);
    }

    @ExceptionHandler
    public JsonResult handleBindException(BindException e) {
        log.debug("全局异常处理器开始处理BindException");
        FieldError fieldError = e.getFieldError();
        String message = fieldError.getDefaultMessage();
        return JsonResult.fail(ServiceCode.ERROR_BAD_REQUEST, message);
    }


    @ExceptionHandler
    public JsonResult handleConstraintViolationException(ConstraintViolationException e) {
        log.debug("全局异常处理器开始处理ConstraintViolationException");
        String message = null;
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
        for (ConstraintViolation<?> constraintViolation : constraintViolations) {
            message = constraintViolation.getMessage();
        }
        return JsonResult.fail(ServiceCode.ERROR_BAD_REQUEST, message);
    }

    @ExceptionHandler({
            InternalAuthenticationServiceException.class,
            BadCredentialsException.class
    })
    public JsonResult handleAuthenticationException(AuthenticationException e){
        log.debug("全局异常处理开始处理AuthenticationException");
        String message = "登录失败，用户名或者密码错误！";
        return JsonResult.fail(ServiceCode.ERROR_UNAUTHORIZED,message);
    }

    @ExceptionHandler
    public JsonResult handleDisableException(DisabledException e){
        log.debug("全局异常处理器开始处理DisabledException");
        String message = "登录失效，账号已经被禁用！";
        return JsonResult.fail(ServiceCode.ERROR_UNAUTHORIZED_DISABLED,message);

    }

    @ExceptionHandler
    public JsonResult handleBindException(AccessDeniedException e) {
        log.debug("全局异常处理器开始处理AccessDeniedException");
        String message ="您当前登录的账号无此操作权限";
        return JsonResult.fail(ServiceCode.ERROR_FORBIDDEN, message);
    }

    /*处理其他异常,用来处理程序运行过程中未知的异常*/
/*    @ExceptionHandler
    public JsonResult handleThrowable(Throwable e) {
        log.debug("全局异常处理器开始处理Throwable");
        log.debug("异常跟踪信息如下：", e);
        String message = "服务器忙，请稍后再试！【同学们，看到这句时，你应该检查服务器端的控制台，并在全局异常处理器中添加处理异常的方法】";
        return JsonResult.fail(ServiceCode.ERROR_UNKNOWN, message);
    }*/
}
